//
//  FristCollectionViewController.swift
//  weibo1
//
//  Created by 李进 on 15/10/18.
//  Copyright © 2015年 李进. All rights reserved.
//

import UIKit
import SnapKit


//定义可重用cell的id
private let collcellId = "collCell"
//定义新特性的cell的个数
private let collCellCount:Int = 4

class FristCollectionViewController: UICollectionViewController {
    
    
    var isStart: Bool?

    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.collectionView!.registerClass(collCell.self, forCellWithReuseIdentifier: collcellId)
       
    }
    /// 构造函数
    ///
    /// - returns: FristCollectionViewController
    init() {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        layout.minimumLineSpacing = 0
        
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    // MARK: UICollectionViewDataSource 数据源方法


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return collCellCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collcellId, forIndexPath: indexPath) as! collCell
    
        cell.backgroundColor = UIColor.redColor()
        
        cell.indexpath = indexPath.item
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        //获取当前的页数
        let pag = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        //进行判断是否为最后一页
        if pag != collCellCount - 1 {
            return
        }
        //时最后一页的话就执行动画
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: pag, inSection: 0)) as! collCell
        
        cell.showANIMA()
    }

}


/// cell类
class collCell :UICollectionViewCell{
    
    /// 当前cell的位置
    var indexpath: Int = 0 {
        
        didSet{
        //这边判断是为了使得最后一页才有开始体验按钮
        indexpath == 3 ? (startBut.hidden = false) : (startBut.hidden = true)
       iconview.image = UIImage(named: "new_feature_\(indexpath + 1)")
    }
}
    /// 构造函数
    ///
    /// - parameter frame: frame
    ///
    /// - returns: collCell
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCell()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /// 设置cell的界面
    func setupCell(){
        //新特性的图片
        iconview = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
        //添加button
        addSubview(iconview)
        
        addSubview(startBut)
        //添加按钮监听
        startBut.addTarget(self, action: "starts", forControlEvents: UIControlEvents.TouchUpInside)
        
        startBut.hidden = true
        
     /// 使用snapuikt框架来实现自动布局
        startBut.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).offset(-200)
        }
    }
    
    /// 点击按钮的响应事件
    func starts(){
        
       // FristCollectionViewController().isStart = true
        
        //添加通知中心
        NSNotificationCenter.defaultCenter().postNotificationName(NotificationCenterName, object: "33")
        
        
    }
    
    /// but的动画显示
    func showANIMA(){
        
        startBut.hidden = false
        startBut.transform = CGAffineTransformMakeScale(0, 0)
        startBut.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.2,//动画时长
            delay: 0,                   //延长时间
            usingSpringWithDamping: 0.1,//弹力系数
            initialSpringVelocity: 10,//初始速度
            options: [],//动画选项
            animations: { () -> Void in
               
                self.startBut.transform = CGAffineTransformIdentity
                
            }) { (_) -> Void in
              
                self.startBut.userInteractionEnabled = true
        }
        
    }
    
     /// 懒加载
    lazy var startBut: UIButton = UIButton(tittle: "开始体验", backImageName: "new_feature_finish_button", fontsize: 18, fontcolcr: UIColor.whiteColor())
    
    lazy var iconview: UIImageView  = UIImageView()
    
}



